package com.ysw.io.读取单一文件并发入库;

import com.ysw.bean.User;
import com.ysw.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @author: yu_sw
 * @Description: com.ysw.io.读取单一文件并发入库
 * @date:2022/5/17 21:26
 */
@Slf4j
public class UserFileTask implements Runnable{
    private CountDownLatch countDownLatch;

    private List<User> users;

    private UserMapper userMapper;

    public UserFileTask(CountDownLatch countDownLatch, List<User> users, UserMapper userMapper){
        this.countDownLatch = countDownLatch;
        this.users = users;
        this.userMapper = userMapper;
    }
    @Override
    public void run() {
        try{
            if(CollectionUtils.isNotEmpty(users)){
                int i = userMapper.insertBatch(users);
                log.info("插入成功：{}", i);
                TimeUnit.SECONDS.sleep(2);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            countDownLatch.countDown();
        }
    }
}
